package models;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;

import play.db.ebean.Model;

@Entity
@Table( name = "user")
public class User extends Model
{
    private static final long serialVersionUID = 1L;
    
    @Id
    protected String 	username ;
    protected String 	password ;
    protected String 	firstname ;
    protected String 	lastname ;
    protected Integer 	type;
    @Column(columnDefinition="LONGBLOB")
    protected byte[]	userpic;
    
    protected final static  String USERNAME  = "username";
    protected final static  String PASSWORD  = "password";
    protected final static  String FIRSTNAME = "firstname";
    protected final static  String LASTNAME  = "lastname";
    
    public final static int ADMINISTRATOR 	= 4;
    public final static int SUPERVISOR 		= 3;
    public final static int WRITER 		= 2;
    public final static int READER		= 1;
    public final static int GUEST		= 0;

    public final static String ADMINISTRATOR_TITLE 	= "Администратор";
    public final static String SUPERVISOR_TITLE 	= "Супервизор";
    public final static String WRITER_TITLE 		= "Редактор";
    public final static String READER_TITLE		= "Читатель";
    public final static String GUEST_TITLE		= "Гость";
    
    public static List<User> getByUserName(String userName)
    {
	try
	{
	    return User.find.where().eq(User.USERNAME, userName).findList();
	    
	}catch(Exception e)
	{
	    return new ArrayList<User>();
	}
    }
    
    public static Model.Finder<Long, User> find = new Model.Finder<Long, User>(
	    Long.class, User.class);

    public String getUsername()
    {
        return username;
    }

    public void setUsername(String username)
    {
        this.username = username;
    }

    public String getPassword()
    {
        return password;
    }

    public void setPassword(String password)
    {
        this.password = password;
    }

    public String getFirstname()
    {
        return firstname;
    }

    public void setFirstname(String firstname)
    {
        this.firstname = firstname;
    }

    public String getLastname()
    {
        return lastname;
    }

    public void setLastname(String lastname)
    {
        this.lastname = lastname;
    }
    
    public static User getUser(String username)
    {
	List<User> user = User.find.where().eq(User.USERNAME, username)
		.findList();
	if (user == null || user.size() != 1) return null;
	return user.get(0);
    }

    public static List<User> getAllUsers()
    {
	List<User> user = User.find.all();
	return user;
    }
    
    public Integer getType()
    {
	//TODO hot fix
        if(null == type)
            type = User.GUEST;
	return type;
    }

    public void setType(Integer type)
    {
        this.type = type;
    }

    public byte[] getUserpic()
    {
        return userpic;
    }

    public void setUserpic(byte[] userpic)
    {
        this.userpic = userpic;
    }
}
